package com.fancy.learncenter.ui.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.AudioBean;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.MyCollectionDataBean;
import com.fancy.learncenter.common.LoginUserSharePrefernce;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.adapter.FragmentCollectionArtAdapter;
import com.fancy.learncenter.ui.adapter.FragmentGoodsActivityAdapter;
import com.fancy.learncenter.utils.SPUtils;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;

import static com.fancy.learncenter.utils.NetUtil.getResResult;

/**
 * A simple {@link Fragment} subclass.
 */
public class CollectionArtFragment extends Fragment {


    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;


    FragmentCollectionArtAdapter listAdapter;


    ArrayList<MyCollectionDataBean.CollectionVOListBean> listData;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_collection_art, container, false);
        ButterKnife.bind(this, view);

        initRecycle();
        getListData();

        return view;
    }

    private void initRecycle() {
        listData = new ArrayList<>();


        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        listAdapter = new FragmentCollectionArtAdapter(getActivity(), listData);
        recyclerView.setAdapter(listAdapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    private void getListData() {
        HttpMehtod.getInstance().getCollection(LoginUserSharePrefernce.getCustomId(), 0, new CustomNetSubscriber(getActivity()) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {
                //解析数据
                BaseBean<MyCollectionDataBean> baseBean = Utils.fromJsonObject(getResResult(responseBodyResponse), MyCollectionDataBean.class);
                //数据业务逻辑处理
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(),baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<MyCollectionDataBean>() {
                    @Override
                    public void success(MyCollectionDataBean dataBean) {
                        listData.removeAll(listData);

                        listData.removeAll(listData);
                        if (dataBean.getCollectionVOList() != null) {
                            listData.addAll(dataBean.getCollectionVOList());
                        }
                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();
            }

            @Override
            public void onCompleted() {
                super.onCompleted();

                listAdapter.notifyDataSetChanged();
            }
        });
    }
}
